//
//  ViewController.m
//  22-AFNetworking
//
//  Created by Dendi on 8/12/16.
//  Copyright © 2016 kaleidoscope. All rights reserved.
//

#import "ViewController.h"
#import "AFNetworking.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
//    [self netWorkGET];
    [self netWorkPOST];
}
#pragma mark - 网络请求的GET请求 AFNetworking
- (void)netWorkGET {
    
    AFHTTPSessionManager *manager = [[AFHTTPSessionManager alloc] init];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    
    //    responseSerializer默认类型AFJSONResponseSerializer
    //    保留系统中ContentType ,添加新的type
    NSMutableSet *set = [NSMutableSet setWithSet:manager.responseSerializer.acceptableContentTypes];
    [set addObject:@"text/html"];
    [manager.responseSerializer setAcceptableContentTypes:set];
    
    //  1. requestSerializer 格式化Body、参数
    manager.requestSerializer = [AFJSONRequestSerializer serializer];
    // 设置请求头 可变的字典存储请求头 设置requestSerializer中的一个字典
    // mutableHTTPRequestHeaders字典
    //    manager.requestSerializer setValue:@"" forHTTPHeaderField:@""]
    // 超时时间 默认60s
    manager.requestSerializer.timeoutInterval = 80;
    
    
    //  2. responseSerializer 返回数据，进行格式化
    NSDictionary *parameters = @{@"name":@"王道林"};
    [manager GET:@"http://app.api.autohome.com.cn/autov4.6/cars/brands-a2-pm2-v4.6.1-ts635580073583652962.html" parameters:parameters progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSLog(@"%@", task.currentRequest.URL);
        NSLog(@"请求成功%@",[responseObject class]);
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"%@", task.currentRequest.URL);
        NSLog(@"请求失败：%@", error);
    }];
}
#pragma mark - 网络请求的POST请求 AFNetworking
- (void)netWorkPOST {
    
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    NSString *postURLString = @"http://open3.bantangapp.com/base/app/init";
    NSString *parameters = @"app_id=com.jzyd.BanTang&app_installtime=1467276561&app_versions=5.8.5&channel_name=appStore&client_id=bt_app_ios&client_secret=9c1e6634ce1c5098e056628cd66a17a5&device_token=1aa2741a8e58dbf700d96e588b551a5bf7f8a1e222eb9948c836ba717ce0d48e&os_versions=10.0&screensize=750&track_device_info=iPhone8%2C1&track_deviceid=2BBAE4F7-2E3B-41AA-AE0C-F91FB2BDC279&v=17";
//    manager.requestSerializer = [AFJSONResponseSerializer serializer];
    // 请求内部做的转码 不用转码 直接调用这个参数
    
//    [manager.requestSerializer setQueryStringSerializationWithBlock:^NSString * _Nonnull(NSURLRequest * _Nonnull request, id  _Nonnull parameters, NSError * _Nullable __autoreleasing * _Nullable error) {
//        return parameters;
//    }];
    [manager POST:postURLString parameters:parameters progress:^(NSProgress * _Nonnull uploadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSLog(@"%@", task.currentRequest.HTTPBody);
        NSLog(@"成功%@", responseObject);
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"失败%@", error);
    }];
}



@end
